var login_user = null
var stuid = null
var mask = null

$(function() { // 初始化内容

	mui.plusReady();
	mui.init();
	mask = mui.createMask(); //遮罩层
	login_user = JSON.parse(localStorage.getItem('loginUser'));
	datetime_init();
	tap_init();



	var star = window.sessionStorage.getItem('stu_id')

	stuid = JSON.parse(star)


	window.setTimeout(function() {
		initFollowUp()
	}, 500);
});

function tap_init() {

	jQuery(document).ready(function() {

		jQuery("#select_fp").change(function() {
			var s2id_autogen2 = jQuery("#select_fp").val()
			if (s2id_autogen2 == "校区") {
				jQuery("#select_school").removeAttr("style")
				jQuery("#select_saler").css("display", "none")
			} else if (s2id_autogen2 == "顾问") {
				jQuery("#select_saler").removeAttr("style")
				jQuery("#select_school").css("display", "none")

			}

		});

	});


}



function saveFollowUp() {
	var redates = jQuery("#redates").val()
	if(redates == ""){
		var data = {}
		data['id'] = jQuery("#id").val();
		data['stuid'] = jQuery("#stuid").val();
		data['stuname'] = jQuery("#stuname").val();
		data['tel'] = jQuery("#tel").val();
		data['servicefollowstatus'] = jQuery("#servicefollowstatus").val();
		data['explain'] = jQuery("#explain").val();
		data['select_fp'] = jQuery("#select_fp").val();
		data['stu_date'] = jQuery("#stu_date").val();
		data['createdate'] = jQuery("#createdate").val();
		data['followid'] = login_user.id
		data['followname'] = login_user.name
		data['personalid'] = jQuery("#personalid").val()
		data['personalname'] = jQuery("#personalname").val()
		data['schoolid'] =jQuery("#schoolid").val();
		data['schoolname'] =jQuery("#schoolname").val();
		data['upschoolid'] =jQuery("#upschoolid").val();
		data['upschoolname'] =jQuery("#upschoolname").val();
	}else{
		var data = jQuery("#new_gj_form").serializeObject();;
		
		data['redate'] = jQuery("#dayredates").val();
		data['followid'] = login_user.id
		data['followname'] = login_user.name
		
	}
	
	console.log('表单上传信息=====' + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/savegj",
		contentType: 'application/json',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	    		

		},
		success: function(result) {

			mask.close(); //显示遮罩层  	

			console.log("获取数据======" + JSON.stringify(result));
			mui.alert("保存成功")
			setTimeout(function() {

				mui.back()
			}, 1000)
		},

		error: function(e) {

			mask.close(); //显示遮罩层

			mui.alert(e)
			console.log(e);
		}
	});

}


//保存
function submitsave() {
	var redates = jQuery("#redates").val();
	if(redates != ""){
		splitDate();
	}else{
		var time = CurentTime();
		jQuery("#createdate").val(time)
		jQuery("#stu_date").val(time)
	}
	
	

	var select_fp = jQuery("#select_fp").val()
	if (select_fp == "校区") {
		var schoolid = $('#school').find("option:selected").data("schoolid")
		jQuery("#schoolid").val(schoolid)
		var schoolname = $('#school').find("option:selected").data("schoolname")
		jQuery("#schoolname").val(schoolname)
		var upschoolid = $('#school').find("option:selected").data("upschoolid")
		jQuery("#upschoolid").val(upschoolid)
		var upschoolname = $('#school').find("option:selected").data("upschoolname")
		jQuery("#upschoolname").val(upschoolname)

	} else {
		var schoolid = $('#saler').find("option:selected").data("schoolid")
		jQuery("#schoolid").val(schoolid)
		var schoolname = $('#saler').find("option:selected").data("schoolname")
		jQuery("#schoolname").val(schoolname)
		var upschoolid = $('#saler').find("option:selected").data("upschoolid")
		jQuery("#upschoolid").val(upschoolid)
		var upschoolname = $('#saler').find("option:selected").data("upschoolname")
		jQuery("#upschoolname").val(upschoolname)
		var personalid = $('#saler').find("option:selected").val()
		jQuery("#personalid").val(personalid)
		var personalname = $('#saler').find("option:selected").data("name")
		jQuery("#personalname").val(personalname)
	}
	saveFollowUp();

}


function splitDate(){
	var dayredate = jQuery("#redates").val().split(" ")[0]
	jQuery("#dayredate").val(dayredate)
	console.log("dayredate=====" + dayredate)
	var redetaildate = jQuery("#redates").val().split(" ")[1]
	jQuery("#redetaildate").val(redetaildate)
	console.log("redetaildate=====" + redetaildate)
}

function initFollowUp() {
	var data = {};
	data['stuid'] = stuid;
	console.log("初始化客户跟进信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/newgj",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {
			mask.close(); //显示遮罩层 	  
			console.log("跟进信息=====" + JSON.stringify(res));
			var stuMsg = res.stuList[0]
			jQuery("#stuid").val(stuMsg.id)
			jQuery("#stuname").val(stuMsg.stuname)
			jQuery("#tel").val(stuMsg.stutel)
			jQuery("#schoolid").val(stuMsg.schoolid)
			jQuery("#schoolname").val(stuMsg.schoolname)
			jQuery("#upschoolid").val(stuMsg.upschoolid)
			jQuery("#upschoolname").val(stuMsg.upschoolname)
			var followList = res.followList
			var yyList = res.yyList
			jQuery("#id").val(res.newxlc_follow_record.id)




		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}
